package springboot.file.demo.service.impl;

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import springboot.file.demo.pojo.vo.BaseVo;
import springboot.file.demo.pojo.vo.DemoExcelVo;
import springboot.file.demo.service.ExportExcelService;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;

/**
 * @Author: jxm
 * @Description:
 * @Date: 2022/10/21 15:28
 * @Version: 1.0
 */
@Service
public class ExportExcelServiceImpl implements ExportExcelService {


    @Override
    public Workbook exportToExcel() {
        //这是表头
        String[] arr = {"名称","年龄"};
        //这是具体数据
        List list1 = new ArrayList<>();
        list1.add("张三");
        list1.add("18");
        List list2 = new ArrayList<>();
        list2.add("李四");
        list2.add("33");
        List<List> list = new ArrayList<>();
        list.add(list1);
        list.add(list2);
//        return ExcelWrite.writeToExcelByList(arr, list);
        return null;
    }

    @Override
    public List<DemoExcelVo> exportData() {

        DemoExcelVo vo1 = new DemoExcelVo();
        vo1.setId("1");
        vo1.setAge(18);
        vo1.setName("zhangsan1");
        vo1.setAlias("z1");
        DemoExcelVo vo2 = new DemoExcelVo();
        vo1.setId("2");
        vo1.setAge(48);
        vo1.setName("zhangsan2");
        vo1.setAlias("z1");
        DemoExcelVo vo3 = new DemoExcelVo();
        vo1.setId("3");
        vo1.setAge(20);
        vo1.setName("zhangsan3");
        vo1.setAlias("z1");
        DemoExcelVo vo4 = new DemoExcelVo();
        vo1.setId("4");
        vo1.setAge(45);
        vo1.setName("zhangsan4");
        vo1.setAlias("z1");

        ArrayList<DemoExcelVo> demoExcelVos = new ArrayList<>();
        demoExcelVos.add(vo1);
        demoExcelVos.add(vo2);
        demoExcelVos.add(vo3);
        demoExcelVos.add(vo4);
        return demoExcelVos;
    }

    @Override
    public List<BaseVo> data() {
        List<BaseVo> list = new ArrayList<>();
        BaseVo vo = new BaseVo();
        vo.setCreateTime("2023-11-11");
        vo.setNumber(1);
        vo.setName("仓库1");
        vo.setAddress("贵阳市观山湖区");
        vo.setApprovalDate(LocalDate.now());
        vo.setArea(3.22);
        vo.setUseArea(1.22);
        vo.setMaxCapacity(10000L);

        vo.setSourceName("来源");
        vo.setPrice(1.55);
        vo.setSystemName("哈哈哈哈哈哈哈哈哈");
        list.add(vo);

        return list;
    }

    @Override
    public List<Object> getData() {
        BaseVo data = data().get(0);
        List<Object> list = new ArrayList<>();

        list.add(data.getNumber());
        list.add(data.getCreateTime());
        list.add(data.getName());
        list.add(data.getAddress());
        list.add(data.getApprovalDate());
        list.add(data.getArea());
        list.add(data.getUseArea());
        list.add(data.getMaxCapacity());
        list.add(data.getSourceName());
        list.add(data.getPrice());
        list.add(data.getSystemName());

        return list;

    }

    public File createExcel(HashMap<String, Object> resultMap,
                            List<Map<String, Object>> actualList,
                            List<Map<String, Object>> netWorkList,
                            HttpServletResponse response){
        /** 第一步，创建一个Workbook，对应一个Excel文件  */
        XSSFWorkbook wb = new XSSFWorkbook();
        /** 第二步，在Workbook中添加一个sheet,对应Excel文件中的sheet  */
        XSSFSheet sheet = wb.createSheet("sheet1");
        //设置每个列有多宽  100为单位好计算
        sheet.setColumnWidth(0,100 * 12);
        sheet.setColumnWidth(1,100 * 19);
        sheet.setColumnWidth(2,100 * 38);
        sheet.setColumnWidth(3,100 * 38);
        sheet.setColumnWidth(4,100 * 38);
        sheet.setColumnWidth(5,100 * 38);
        sheet.setColumnWidth(6,100 * 38);

        // 行号
        int rowNum = 0;
        /** 创建第一页的第一行，索引从0开始*/
        XSSFRow row0 = sheet.createRow(rowNum++);
        String title = "序号";
        //单元格
        XSSFCell c00 = row0.createCell(0);
        XSSFCell c01 = row0.createCell(1);
        XSSFCell c02 = row0.createCell(2);
        XSSFCell c03 = row0.createCell(3);
        XSSFCell c04 = row0.createCell(4);

        return null;
    }
}
